package org.csu.onlineedubackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.onlineedubackend.dto.ForumFollowDTO;
import org.csu.onlineedubackend.entity.ForumFollow;
import org.csu.onlineedubackend.entity.User;
import org.csu.onlineedubackend.mapper.ForumFollowMapper;
import org.csu.onlineedubackend.mapper.UserMapper;
import org.csu.onlineedubackend.service.ForumFollowService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 論壇關注服務實現類
 */
@Service
public class ForumFollowServiceImpl implements ForumFollowService {

    @Autowired
    private ForumFollowMapper forumFollowMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean follow(Integer followerId, Integer followingId) {
        // 檢查是否已經關注
        if (isFollowing(followerId, followingId)) {
            return false;
        }
        
        // 檢查用戶是否存在
        User follower = userMapper.selectById(followerId);
        User following = userMapper.selectById(followingId);
        if (follower == null || following == null) {
            return false;
        }
        
        // 不能關注自己
        if (followerId.equals(followingId)) {
            return false;
        }
        
        ForumFollow forumFollow = new ForumFollow();
        forumFollow.setFollowerId(followerId);
        forumFollow.setFollowingId(followingId);
        
        return forumFollowMapper.insert(forumFollow) > 0;
    }

    @Override
    public boolean unfollow(Integer followerId, Integer followingId) {
        QueryWrapper<ForumFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", followerId)
                   .eq("following_id", followingId);
        
        return forumFollowMapper.delete(queryWrapper) > 0;
    }

    @Override
    public boolean isFollowing(Integer followerId, Integer followingId) {
        QueryWrapper<ForumFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", followerId)
                   .eq("following_id", followingId);
        
        return forumFollowMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public List<ForumFollowDTO> getFollowingList(Integer userId) {
        QueryWrapper<ForumFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", userId)
                   .orderByDesc("created_at");
        
        List<ForumFollow> forumFollows = forumFollowMapper.selectList(queryWrapper);
        
        return forumFollows.stream().map(this::convertToDTO).collect(Collectors.toList());
    }

    @Override
    public List<ForumFollowDTO> getFollowerList(Integer userId) {
        QueryWrapper<ForumFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("following_id", userId)
                   .orderByDesc("created_at");
        
        List<ForumFollow> forumFollows = forumFollowMapper.selectList(queryWrapper);
        
        return forumFollows.stream().map(this::convertToDTO).collect(Collectors.toList());
    }

    @Override
    public int getFollowingCount(Integer userId) {
        QueryWrapper<ForumFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", userId);
        
        return Math.toIntExact(forumFollowMapper.selectCount(queryWrapper));
    }

    @Override
    public int getFollowerCount(Integer userId) {
        QueryWrapper<ForumFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("following_id", userId);
        
        return Math.toIntExact(forumFollowMapper.selectCount(queryWrapper));
    }

    /**
     * 轉換為 DTO
     */
    private ForumFollowDTO convertToDTO(ForumFollow forumFollow) {
        ForumFollowDTO dto = new ForumFollowDTO();
        BeanUtils.copyProperties(forumFollow, dto);
        
        // 獲取用戶信息
        User follower = userMapper.selectById(forumFollow.getFollowerId());
        User following = userMapper.selectById(forumFollow.getFollowingId());
        
        if (follower != null) {
            dto.setFollowerName(follower.getUsername());
            dto.setFollowerAvatar(follower.getAvatar());
        }
        
        if (following != null) {
            dto.setFollowingName(following.getUsername());
            dto.setFollowingAvatar(following.getAvatar());
        }
        
        return dto;
    }
} 